﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace EyFramework.ThirdPart.IdCard
{
    public class IdCardService
    {
        public static bool ExistIDCard()
        {
            // 初始化端口
            int iPort = 1001;
            int intOpenRet = -1;
            for (; iPort < 1017; iPort = iPort + 1)
            {
                intOpenRet = InitComm(iPort);
                if (intOpenRet == 1)
                {
                    break;
                }
            }
            if (intOpenRet != 1)
            {
                throw new Exception("端口打开失败");
            }
            int ires = CardOn();
            // 设定当前操作的 HID 接口 iDR210
            if (HIDSelect(iPort))
            {
                int intReadRet = Authenticate();
                if (intReadRet == 1)
                {
                    return true;
                }
            }
            return false;
        }

        public static IdCardInfo ReadIDCard()
        {
            StringBuilder Name = new StringBuilder(31);
            StringBuilder Gender = new StringBuilder(3);
            StringBuilder Folk = new StringBuilder(10);
            StringBuilder BirthDay = new StringBuilder(9);
            StringBuilder Code = new StringBuilder(19);
            StringBuilder Address = new StringBuilder(71);
            StringBuilder Agency = new StringBuilder(31);
            StringBuilder ExpireStart = new StringBuilder(9);
            StringBuilder ExpireEnd = new StringBuilder(9);
            // 初始化端口
            int iPort = 1001;
            int intOpenRet = -1;
            for (; iPort < 1017; iPort = iPort + 1)
            {
                intOpenRet = InitComm(iPort);
                if (intOpenRet == 1)
                {
                    break;
                }
            }
            if (intOpenRet != 1)
            {
                throw new Exception("端口打开失败");
            }
            int ires = CardOn();
            // 设定当前操作的 HID 接口 iDR210
            if (HIDSelect(iPort))
            {
                // 卡认证
                int intReadRet = Authenticate();
                IdCardInfo CardData = new IdCardInfo();
                /*if (intReadRet != 1)
                {
                    throw new Exception("卡认证失败");
                }*/
                // 读身份证信息
                int intReadBaseInfosRet = ReadBaseInfos(Name, Gender, Folk, BirthDay, Code,
                Address, Agency, ExpireStart, ExpireEnd);
                if (intReadBaseInfosRet != 1)
                {
                    throw new Exception("读取身份证信息失败");
                }

                CardData.CardNo = Code.ToString().Trim();
                CardData.Name = Name.ToString().Trim();
                CardData.Sex = Gender.ToString().Trim();
                CardData.Nationality = Folk.ToString().Trim();
                CardData.Birthday = BirthDay.ToString().Trim();
                CardData.Address = Address.ToString().Trim();
                CardData.Institution = Agency.ToString().Trim();
                CardData.Begin_validity = ExpireStart.ToString().Trim();
                CardData.End_validity = ExpireEnd.ToString().Trim();
                return CardData;
            }
            else
            {
                throw new Exception("读取身份证信息失败");
            }
        }

        #region 导入Dll

        [DllImport("sdtapi.dll")]
        public static extern int InitComm(int Port);// 初始化

        [DllImport("Sdtapi.dll")]
        public static extern int Authenticate();// 卡认证

        [DllImport("sdtapi.dll")]
        public static extern void CloseComm(); // 关闭端口

        [DllImport("sdtapi.dll")]
        public static extern int ReadBaseInfos(StringBuilder Name, StringBuilder Gender, StringBuilder Folk, StringBuilder BirthDay, StringBuilder Code, StringBuilder Address, StringBuilder Agency, StringBuilder ExpireStart, StringBuilder ExpireEnd);// 读取数据

        [DllImport("sdtapi.dll")]
        public static extern int HID_BeepLED(bool BeepON, bool LEDON, int duration); // 蜂鸣器

        [DllImport("sdtapi.dll")]
        public static extern bool HIDSelect(int index); // 设定当前操作的 HID 接口 iDR210

        [DllImport("sdtapi.dll")]
        public static extern int CardOn(); // 判断身份证是否在设备上

        #endregion 导入Dll
    }
}